package com.edushare.activity;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.edushare.R;
import com.edushare.controller.CommentFunctions;
import com.edushare.controller.UserFunctions;
import com.edushare.entity.Comment;
import com.edushare.entity.Globals;

public class DrawCommentActivity extends Activity {
	
	private Button commentBtn;
	private EditText commentTxt;
	private ListView commentItems;

	ListAdapter commentAdaptor;
	ArrayList<Comment> commentList = new ArrayList<Comment>();
	ArrayList<String> commentStrList = new ArrayList<String>();

	HashMap<String, String> userDetails;
	String username;
	String userID;

	CommentFunctions commentFunction = new CommentFunctions();
	UserFunctions userFunction = new UserFunctions();
	String mediaID;

//	private void addComment(Comment comment) {
//		String username = comment.getUsername();
//		String commentStr = comment.getComment();
//		commentStrList.add(username + ": " + commentStr);
//		commentAdaptor.notifyDataSetChanged();
//		commentTxt.setText(" ");
//	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.draw_comment);

		commentItems = (ListView) findViewById(R.id.commentListView);
		commentTxt = (EditText) findViewById(R.id.commentTxt);
		commentBtn = (Button) findViewById(R.id.commentBtn);
		
		// get media ID
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			mediaID = null;
		} else {
			mediaID = extras.getString("mediaID");
		}
		
		// retrieve comments
		commentList = commentFunction.retrieveComments(mediaID);

		if(commentList != null){
			commentAdaptor = new ListAdapter(DrawCommentActivity.this);
			commentItems.setAdapter(commentAdaptor);
			commentAdaptor.notifyDataSetChanged();
		}
		
	
	

	
		UserFunctions userFn = new UserFunctions();
		if (userFn.isUserLoggedIn(getApplicationContext())) {
			Globals.uid = userFn.getUid(getApplicationContext());
			userDetails = userFn.getUser(getApplicationContext());
			username = userDetails.get("name");
			userID = userDetails.get("id");
		}

		commentBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				String commentStr = commentTxt.getText().toString();
				String groupMediaID = mediaID;

				JSONObject json = commentFunction.addComment(username,
						groupMediaID, commentStr);
				try {
					if (json.get("success") != null) {
						int success = json.getInt("success");
						String msg = json.getString("message");
						if (success == 1) {
							Toast.makeText(getApplicationContext(), msg,
									Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(getApplicationContext(),
									"not added", Toast.LENGTH_SHORT).show();
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}

				// retrieve comments
				commentList = commentFunction.retrieveComments(mediaID);

				if(commentList != null){
					commentAdaptor = new ListAdapter(DrawCommentActivity.this);
					commentItems.setAdapter(commentAdaptor);
					commentAdaptor.notifyDataSetChanged();
				}

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.draw_comment, menu);
		return true;
	}
	
	private class ListAdapter extends BaseAdapter {

		LayoutInflater inflater;
		ViewHolder viewHolder;

		public ListAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return commentList.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			parent.invalidate();
			convertView = inflater.inflate(R.layout.list_row_draw_comment, null);
			viewHolder = new ViewHolder();

			viewHolder.usernameTV = (TextView) convertView
					.findViewById(R.id.commentUsername);
			viewHolder.commentTV = (TextView) convertView
					.findViewById(R.id.commentDesc);

			convertView.setTag(viewHolder);

			viewHolder.usernameTV.setText(commentList.get(position).getUsername());
			viewHolder.commentTV.setText(commentList.get(position).getComment());

			return convertView;

		}
	}
	
	private class ViewHolder {
		public TextView usernameTV;
		public TextView commentTV;
	}
	

}
